/*
   Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package org.latestbit.smartobjects.coders.tests;

import java.nio.ByteBuffer;
import static org.junit.Assert.*;

import org.junit.Test;
import org.latestbit.smartobjects.coders.*;
import org.latestbit.smartobjects.io.ResizableByteBuffer;
import org.latestbit.smartobjects.network.IPacketFormer;


public class StdPacketFormerTest {

	@Test
	public void testStdPacketFormerCallReq() throws Exception {
		IPacketFormer former = new StdPacketFormer();
		IDataTypesCoder coder = new StdDataTypesCoder();
		ResizableByteBuffer writeBuffer = new ResizableByteBuffer(false, 100, 100, 0);
		CallInterfaceOperationRequest callCommand = new CallInterfaceOperationRequest(writeBuffer, coder);
		callCommand.setDestination("TestInterface");
		callCommand.setInstanceId("0102030405");
		callCommand.setMessageId(10);
		callCommand.setOperationId("testOperation");
		callCommand.beginSerialize();
		callCommand.finishSerialize();		
		
		former.onReceiveRawPacket(writeBuffer.getBuffer());
		ByteBuffer readyPacket = former.getReadyPacket();
		assertNotNull(readyPacket);
		assertNull(former.getReadyPacket());
		
		readyPacket.mark();
		StdNetworkCommand command = new StdNetworkCommand(readyPacket, coder);
		command.deserialize();
		assertTrue( command.isRequest() );
		assertEquals( CallInterfaceOperationRequest.COMMAND_ID, command.getCommandId() );
		CallInterfaceOperationRequest deserializedRequest = new CallInterfaceOperationRequest(command);
		deserializedRequest.deserialize();
		assertEquals( callCommand.getDestination(), deserializedRequest.getDestination() );
		assertEquals( callCommand.getInstanceId(), deserializedRequest.getInstanceId() );
		assertEquals( callCommand.getMessageId(), deserializedRequest.getMessageId() );
		assertEquals( callCommand.getOperationId(), deserializedRequest.getOperationId() );
		readyPacket.reset();
		
		writeBuffer.getBuffer().clear();
		
		callCommand.setInstanceId(null);
		callCommand.beginSerialize();
		callCommand.finishSerialize();
		former.onReceiveRawPacket(writeBuffer.getBuffer());
		readyPacket = former.getReadyPacket();
		assertNotNull(readyPacket);
		assertNull(former.getReadyPacket());
		
		readyPacket.mark();
		command = new StdNetworkCommand(readyPacket, coder);
		command.deserialize();
		assertTrue(command.isRequest());
		assertEquals( CallInterfaceOperationRequest.COMMAND_ID, command.getCommandId());
		deserializedRequest = new CallInterfaceOperationRequest(command);
		deserializedRequest.deserialize();
		assertNull( callCommand.getInstanceId());
		assertEquals( callCommand.getDestination(), deserializedRequest.getDestination() );
		assertEquals( callCommand.getMessageId(), deserializedRequest.getMessageId() );
		assertEquals( callCommand.getOperationId(), deserializedRequest.getOperationId() );
		readyPacket.reset();
	}
	
	@Test
	public void testStdPacketFormerCallResponse() throws Exception {
		IPacketFormer former = new StdPacketFormer();
		IDataTypesCoder coder = new StdDataTypesCoder();
		ResizableByteBuffer writeBuffer = new ResizableByteBuffer(false, 100, 100, 0);
		CallInterfaceOperationResponse callCommand = new CallInterfaceOperationResponse(writeBuffer, coder);
		callCommand.setDestination("TestInterface");
		callCommand.setInstanceId("0102030405");
		callCommand.setMessageId(10);
		callCommand.beginSerialize();
		callCommand.finishSerialize();
		
		former.onReceiveRawPacket(writeBuffer.getBuffer());
		ByteBuffer readyPacket = former.getReadyPacket();
		assertNotNull(readyPacket);
		
		coder.reset();
		StdNetworkCommand command = new StdNetworkCommand(readyPacket, coder);
		command.deserialize();
		assertTrue(command.isResponse());
		assertEquals( CallInterfaceOperationResponse.COMMAND_ID, command.getCommandId());
		CallInterfaceOperationResponse deserializedResponse = new CallInterfaceOperationResponse(command);
		deserializedResponse.deserialize();
		assertEquals( callCommand.getMessageId(), deserializedResponse.getMessageId() );				
	}
	
	@Test
	public void testStdPacketFormerValidity() throws Exception {
		IPacketFormer former = new StdPacketFormer();
		IDataTypesCoder coder = new StdDataTypesCoder();
		ResizableByteBuffer writeBuffer = new ResizableByteBuffer(false, 100, 100, 0);
		CallInterfaceOperationRequest callCommand = new CallInterfaceOperationRequest(writeBuffer, coder);
		callCommand.setDestination("TestInterface");
		callCommand.setInstanceId("0102030405");
		callCommand.setMessageId(10);
		callCommand.setOperationId("testOperation");
		callCommand.beginSerialize();
		callCommand.finishSerialize();
		
		ResizableByteBuffer writeAllBuffer = new ResizableByteBuffer(false, 1000, 1000, 0);
		for(int i=0;i<10;i++) {
			writeBuffer.getBuffer().mark();
			writeAllBuffer.put(writeBuffer.getBuffer());
			writeBuffer.getBuffer().reset();
		}
		// A half packet
		writeBuffer.getBuffer().mark();
		writeAllBuffer.put(writeBuffer.getBuffer().array(), 0, 32);
		writeBuffer.getBuffer().reset();
		writeAllBuffer.getBuffer().flip();
		former.onReceiveRawPacket(writeAllBuffer.getBuffer());
		
		for(int i=0;i<10;i++) {
			ByteBuffer readyPacket = former.getReadyPacket();
			assertNotNull(readyPacket);
		}
		
		
		// 2part of the half packet
		writeAllBuffer.getBuffer().clear();
		writeBuffer.getBuffer().mark();
		writeAllBuffer.put(writeBuffer.getBuffer().array(), 32, writeBuffer.getBuffer().limit()-32);
		writeBuffer.getBuffer().reset();
		for(int i=0;i<10;i++) {
			writeBuffer.getBuffer().mark();
			writeAllBuffer.put(writeBuffer.getBuffer());
			writeBuffer.getBuffer().reset();
		}		
		writeAllBuffer.getBuffer().flip();
		former.onReceiveRawPacket(writeAllBuffer.getBuffer());
		for(int i=0;i<11;i++) {
			ByteBuffer readyPacket = former.getReadyPacket();
			assertNotNull(readyPacket);
		}
		assertNull(former.getReadyPacket());

		for(int incHeaderSize=1;incHeaderSize<=StdNetworkCommand.HEADER_SIZE+15;incHeaderSize++) {
			writeAllBuffer.getBuffer().clear();
			for(int i=0;i<10;i++) {
				writeBuffer.getBuffer().mark();
				writeAllBuffer.put(writeBuffer.getBuffer());
				writeBuffer.getBuffer().reset();
			}
						
			// A half packet with incomplete header
			writeBuffer.getBuffer().mark();
			writeAllBuffer.put(writeBuffer.getBuffer().array(), 0, incHeaderSize);
			writeBuffer.getBuffer().reset();
			writeAllBuffer.getBuffer().flip();
			former.onReceiveRawPacket(writeAllBuffer.getBuffer());
			
			for(int i=0;i<10;i++) {
				ByteBuffer readyPacket = former.getReadyPacket();
				assertNotNull(readyPacket);
			}
			
			// 2part of the half packet
			writeAllBuffer.getBuffer().clear();
			writeBuffer.getBuffer().mark();
			writeAllBuffer.put(writeBuffer.getBuffer().array(), incHeaderSize, writeBuffer.getBuffer().limit()-incHeaderSize);
			writeBuffer.getBuffer().reset();
			for(int i=0;i<10;i++) {
				writeBuffer.getBuffer().mark();
				writeAllBuffer.put(writeBuffer.getBuffer());
				writeBuffer.getBuffer().reset();
			}		
			writeAllBuffer.getBuffer().flip();
			former.onReceiveRawPacket(writeAllBuffer.getBuffer());
			for(int i=0;i<11;i++) {
				ByteBuffer readyPacket = former.getReadyPacket();
				assertNotNull(readyPacket);
			}
			assertNull(former.getReadyPacket());
		}		
	}

}
